/*
 * PKL 51 GUNUNG KIDUL
 * Subseksie DATA ENTRY + CAPI
 */
package pkl51.controller;

import java.util.logging.Level;
import java.util.logging.Logger;
import pkl51.error.EntityException;
import pkl51.model.Blok123Model;
import pkl51.view.kuesioner.PanelBlok123;

/**
 *
 * @author Hindarwan
 */
public class Blok123Controller {

    private Blok123Model model;

    public void setModel(Blok123Model model) {
        this.model = model;
    }

    public void insertBlok123(PanelBlok123 panelBlok123) throws EntityException{
        try {

            String B1R2 = panelBlok123.getTextFieldB1R2().getText();
            String B1R3 = panelBlok123.getTextFieldB1R3().getText();
            String B1R4 = panelBlok123.getTextFieldB1R4().getText();
            String B1R5 = panelBlok123.getTextFieldB1R5().getText();
            String B1R6 = panelBlok123.getTextFieldB1R6().getText();
            String NKS = panelBlok123.getTextFieldNKS().getText();            
            String B1R8 = panelBlok123.getTextFieldB1R8().getText();

            String B2R1 = panelBlok123.getTextFieldB2R1().getText();
            String B2R2 = panelBlok123.getTextFieldB2R2().getText();
            String B2R3 = panelBlok123.getTextFieldB2R3().getText();

            String B3R1 = panelBlok123.getTextFieldB3R1().getText();
            String B3R2a = panelBlok123.getTextFieldB3R2a().getText();
            String B3R2b = panelBlok123.getTextFieldB3R2b().getText();
            String B3R4 = panelBlok123.getTextFieldB3R4().getText();
            String B3R5a = panelBlok123.getTextFieldB3R5a().getText();
            String B3R5b = panelBlok123.getTextFieldB3R5b().getText();

            model.setForDB(B1R2, B1R3, B1R4, B1R5, B1R6, NKS, B1R8, B2R1, B2R2, B2R3, B3R1, B3R2a, B3R2b, B3R4, B3R5a, B3R5b);

            try {
                model.insertBlok1();
            } catch (EntityException ex) {
                Logger.getLogger(Blok123Controller.class.getName()).log(Level.SEVERE, null, ex);
                throw new EntityException("Insert data blok 1 gagal");
            }
            try {
                model.insertBlok2();
            } catch (EntityException ex) {
                Logger.getLogger(Blok123Controller.class.getName()).log(Level.SEVERE, null, ex);
                throw new EntityException("Insert data blok 2 gagal");
            }
            try {
                model.insertBlok3();
            } catch (EntityException ex) {
                Logger.getLogger(Blok123Controller.class.getName()).log(Level.SEVERE, null, ex);                
                throw new EntityException("Insert data blok 3 gagal");
            }

        } catch (Exception ex) {
            Logger.getLogger(Blok123Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updateBlok123(PanelBlok123 panelBlok123) throws EntityException{
        try {
            String B1R2 = panelBlok123.getTextFieldB1R2().getText();
            String B1R3 = panelBlok123.getTextFieldB1R3().getText();
            String B1R4 = panelBlok123.getTextFieldB1R4().getText();
            String B1R5 = panelBlok123.getTextFieldB1R5().getText();
            String B1R6 = panelBlok123.getTextFieldB1R6().getText();
            String NKS = panelBlok123.getTextFieldNKS().getText();            
            String B1R8 = panelBlok123.getTextFieldB1R8().getText();

            String B2R1 = panelBlok123.getTextFieldB2R1().getText();
            String B2R2 = panelBlok123.getTextFieldB2R2().getText();
            String B2R3 = panelBlok123.getTextFieldB2R3().getText();

            String B3R1 = panelBlok123.getTextFieldB3R1().getText();
            String B3R2a = panelBlok123.getTextFieldB3R2a().getText();
            String B3R2b = panelBlok123.getTextFieldB3R2b().getText();
            String B3R4 = panelBlok123.getTextFieldB3R4().getText();
            String B3R5a = panelBlok123.getTextFieldB3R5a().getText();
            String B3R5b = panelBlok123.getTextFieldB3R5b().getText();

            model.setForDB(B1R2, B1R3, B1R4, B1R5, B1R6, NKS, B1R8, B2R1, B2R2, B2R3, B3R1, B3R2a, B3R2b, B3R4, B3R5a, B3R5b);

            try {
                model.updateBlok1();
            } catch (EntityException ex) {
                Logger.getLogger(Blok123Controller.class.getName()).log(Level.SEVERE, null, ex);                
                throw new EntityException("Update data blok 1 gagal");
            }
            try {
                model.updateBlok2();
            } catch (EntityException ex) {
                Logger.getLogger(Blok123Controller.class.getName()).log(Level.SEVERE, null, ex);                
                throw new EntityException("Update data blok 2 gagal");
            }
            try {
                model.updateBlok3();
            } catch (EntityException ex) {
                Logger.getLogger(Blok123Controller.class.getName()).log(Level.SEVERE, null, ex);                
                throw new EntityException("Update data blok 3 gagal");
            }

        } catch (Exception ex) {
            Logger.getLogger(Blok123Controller.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void getBlok123ByNKS(PanelBlok123 panelBlok123) throws EntityException{
        try {
            model.getBlok123();

            panelBlok123.getTextFieldB1R2().setText(model.getB1R2());
            panelBlok123.getLabelNama_Kec().setText(model.getNama_kec());
            panelBlok123.getTextFieldB1R3().setText(model.getB1R3());
            panelBlok123.getLabelNama_desa().setText(model.getNama_desa());
            panelBlok123.getTextFieldB1R4().setText(model.getB1R4());
            panelBlok123.getTextFieldB1R5().setText(model.getB1R5());
            panelBlok123.getTextFieldB1R6().setText(model.getB1R6());
            panelBlok123.getTextFieldNKS().setText(model.getNKS());
            panelBlok123.getTextFieldB1R8().setText(model.getB1R8());

            panelBlok123.getTextFieldB2R1().setText(model.getB2R1());
            panelBlok123.getTextFieldB2R2().setText(model.getB2R2());
            panelBlok123.getTextFieldB2R3().setText(model.getB2R3());

            panelBlok123.getTextFieldB3R1().setText(model.getB3R1());
            panelBlok123.getLabelNamaPencacah().setText(model.getNama_pencacah());
            panelBlok123.getTextFieldB3R4().setText(model.getB3R4());
            panelBlok123.getLabelNamaKortim().setText(model.getNama_kortim());
            panelBlok123.getTextFieldB3R2a().setText(model.getB3R2a());
            panelBlok123.getTextFieldB3R2b().setText(model.getB3R2b());
            panelBlok123.getTextFieldB3R5a().setText(model.getB3R5a());
            panelBlok123.getTextFieldB3R5b().setText(model.getB3R5b());

        } catch (EntityException ex) {
            Logger.getLogger(Blok123Controller.class.getName()).log(Level.SEVERE, null, ex);            
            throw new EntityException("Data blok 1, 2, 3 tidak ditemukan");

        }
    }
}
